import { apiOrderUpDownValid } from '../../other/api'

const { regeneratorRuntime } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    handleRoute: global.handleRoute,
    // 准备预览图
    async prepareEffectImage(data) {
      return await dispatch(global.globalReducer.prepareEffectImage(data))
    },
    // 获取订单详情
    async getOrderDetailByOrderNo(data) {
      return await dispatch(global.activityReducer.getOrderDetailByOrderNo(data))
    },
    // 获取活动详情
    async getActivityDetail(data) {
      return await dispatch(global.activityReducer.getActivityDetail(data))
    },
    // 审核订单
    async postAuditOrder(data) {
      return await dispatch(global.activityReducer.postAuditOrder(data))
    },
    // 订单状态类别
    async getOrderStatusList(data) {
      return await dispatch(global.activityReducer.getOrderStatusList(data))
    },
    // 高空审核
    async postAuditAirOrder(data) {
      return await dispatch(global.activityReducer.postAuditAirOrder(data))
    },
  })
)({
  data: {
    uiTabIndex: 0,
    uiShowSearch: false,
    uiMessage: '请填写不通过的原因',
    uiOrderType: 0,
    uiSteps: [
      { name: '物料下单', time: '2018-01-28  13:00', status: 1 },
      { name: '订单审核通过', time: '2018-01-28  13:00', status: 1 },
      { name: '订单已接受', time: '2018-01-28  13:00', status: 1 },
      { name: '师傅已到店', time: '', status: 0, uname: '唐某某', mobile: '18674520020' },
      { name: '上画待验收', time: '', status: 0 },
      { name: '下画待验收', time: '', status: 0 },
      { name: '订单完成', time: '', status: 0 },
    ],
    uiReason: '',
    uiReason2: '',

    opt1: {
      uiUserType: '',
      name: '',
      mobile: ''
    },
    showPP: false
  },
  onLoad(option) {
    console.log('activity detail onLoad')
    const { activity_id, order_no } = option
    this.getActivityDetail({ activity_id })
    this.getOrderDetailByOrderNo({ order_no })
    this.getOrderStatusList({ order_no })

    // this.handleOpt(true)
  },
  // onShareAppMessage: function (res) {
  //   return {
  //     title: '某某店活动物料订单详情某某店活动物料订单详情某某店活动物料订单详情某某店活动物料订单详情',
  //     path: '/page/user?id=123',
  //     imageUrl: '/assets/img/logo2@2x.png',
  //     success: function(res) {
  //       // 转发成功
  //     },
  //     fail: function(res) {
  //       // 转发失败
  //     }
  //   }
  // },
  handleShowOpt() {
    console.log()
    this.setData({
      opt1: {
        uiUserType: '项目负责人',
        name: this.data.orderDetail.data.audit_name,
        mobile: this.data.orderDetail.data.audit_mobile
      }
    })
    this.selectComponent('#pop1').handleShow()
  },
  handleHideOpt() {
    this.selectComponent('#pop1').handleHide()
  },
  handleRicon1Tap(){
    this.setData({ showPP: !this.data.showPP });
  },
  handleMakeCall(e) {
    console.log('handleMakeCall', e)
    const { detail } = e
    if (detail && detail.mobile) {
      const phoneNumber = detail.mobile
      wx.makePhoneCall({
        phoneNumber,
        success(e) {

        },
        fail(e) {
          console.error(e)
          if (e.errMsg !== 'makePhoneCall:fail cancel') {
            console.error(e)
          }
        },
      })
    }
  },
  handleImageView(e) {
    const { activity_image_url, activity_image_name } = e.currentTarget.dataset.detail
    const urls = [{
      name: activity_image_name,
      url: activity_image_url,
    }]
    this.prepareEffectImage(urls)
    global.handleRoute({ route: 'effect' })
  },
  handleInput(e) {
    // console.log('handleInput', e)
    const { field } = e.currentTarget.dataset
    const { value } = e.detail
    const obj = {}
    obj[field] = value
    this.setData(obj)
  },
  handleDialog1Cancel(e) {
    this.selectComponent('#dialog1').handleHide()
  },
  handleDialog2Cancel(e) {
    this.selectComponent('#dialog2').handleHide()
  },
  handleDialog1Confirm(e) {
    if (!this.data.uiReason) {
      this.selectComponent('#toast').handleShow({
        message: '请填写不通过的原因'
      })
      return
    }
    this.handleDialog1Cancel()
    this.postAuditOrder({
      order_no: this.data.orderDetail.data.order_no,
      user_id: this.data.minaLoginRes.data.user_id,
      status: 2,
      reason: this.data.uiReason
    }).then(res => {
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '操作失败，请稍后再试'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: res.apiMes || '操作失败，请稍后再试'
      })
    })
  },
  // 确认上画审核通过
  handleDialog2Confirm(e) {
    console.log('handleDialog2Confirm')
    const { order_no } = this.data.orderDetail.data
    const user_id = this.data.minaLoginRes.data.user_id
    const type = 1
    const formData = { order_no, user_id, type }
    const stepName = '确认通过上画审核'
    apiOrderUpDownValid(formData).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || `${stepName}成功`
        })
        this.getOrderDetailByOrderNo({ order_no })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || `${stepName}失败，请稍后再试`
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || `${stepName}失败，请稍后再试`
      })
    })
    this.selectComponent('#dialog2').handleHide()
    // this.handleDialog2Cancel()
  },
  // 确认下画审核通过
  handleDialog3Confirm(e) {
    console.log('handleDialog3Confirm')
    const { order_no } = this.data.orderDetail.data
    const user_id = this.data.minaLoginRes.data.user_id
    const type = 2
    const formData = { order_no, user_id, type }
    const stepName = '确认通过下画审核'
    apiOrderUpDownValid(formData).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || `${stepName}成功`
        })
        this.getOrderDetailByOrderNo({ order_no })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || `${stepName}失败，请稍后再试`
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || `${stepName}失败，请稍后再试`
      })
    })
    this.selectComponent('#dialog3').handleHide()
    // this.handleDialog3Cancel()
  },

  handleDialog5Cancel(e) {
    this.selectComponent('#dialog5').handleHide()
  },
  handleDialog6Cancel(e) {
    this.selectComponent('#dialog6').handleHide()
  },
  // 
  handleDialog6Confirm(e) {
    if (!this.data.uiReason2) {
      this.selectComponent('#toast').handleShow({
        message: '请填写不通过的原因'
      })
      return
    }
    this.handleDialog6Cancel()
    this.postAuditAirOrder({
      order_no: this.data.orderDetail.data.order_no,
      user_id: this.data.minaLoginRes.data.user_id,
      status: 2,
      reason: this.data.uiReason2
    }).then(res => {
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '操作失败，请稍后再试'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: res.apiMes || '操作失败，请稍后再试'
      })
    })
  },
  handleLicon1Tap(e) {
    wx.navigateBack()
  },
  handleTabTap(e) {
    this.setData({
      uiTabIndex: e.currentTarget.dataset.index
    })
  },
  handleOpenDialog1(e) {
    this.selectComponent('#dialog1').handleShow()
  },
  handleOpenDialog6(e) {
    this.selectComponent('#dialog6').handleShow()
  },
  handleOrderStatus(e) {
    const { status } = e.currentTarget.dataset
    switch (status) {
      case 0:
        this.selectComponent('#dialog2').handleShow(null, {
          message: '确定通过上画审核'
        })
        break;
      case 1:
        this.selectComponent('#dialog3').handleShow(null, {
          message: '确定通过下画审核'
        })
        break;
      default:
        break;
    }
  },
  // 确认过审
  handleDialog4Confirm(e) {
    this.selectComponent('#dialog4').handleHide()
    this.postAuditOrder({
      order_no: this.data.orderDetail.data.order_no,
      user_id: this.data.minaLoginRes.data.user_id,
      status: 3,
      reason: ''
    }).then(res => {
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '操作失败，请稍后再试'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: res.apiMes || '操作失败，请稍后再试'
      })
    })
  },
  handleDialog5Confirm(e) {
    this.selectComponent('#dialog5').handleHide()
    this.postAuditAirOrder({
      order_no: this.data.orderDetail.data.order_no,
      user_id: this.data.minaLoginRes.data.user_id,
      status: 3,
      reason: ''
    }).then(res => {
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '操作失败，请稍后再试'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: res.apiMes || '操作失败，请稍后再试'
      })
    })
  },
  // 通过审核
  handlePostAuditOrder(e) {
    console.log('handlePostAuditOrder', e)
    this.selectComponent('#dialog4').handleShow(null, {
      // multiple: true,
      message: [
        `确定审核通过`,
        `${this.data.orderDetail.data.store_name}`,
        `${this.data.orderDetail.data.activity_name}?`,
      ]
      // message: `确定审核通过${this.data.orderDetail.data.store_name}${this.data.orderDetail.data.activity_name}?`
    })
  },
  // 确认通过高空审核
  handlePostAuditAirOrder(e) {
    console.log('handlePostAuditAirOrder', e)
    this.selectComponent('#dialog5').handleShow(null, {
      // multiple: true,
      message: [
        `确定审核通过`,
        `${this.data.orderDetail.data.store_name}`,
        `${this.data.orderDetail.data.activity_name}?`,
      ]
      // message: `确定审核通过${this.data.orderDetail.data.store_name}${this.data.orderDetail.data.activity_name}?`
    })
  },
  // 修改订单
  handleReMakeOrder(e) {
    console.log('handleReMakeOrder', e)
    global.handleRoute({
      route: 'make-order-step1',
      query: {
        order_no: this.data.orderDetail.data.order_no,
        store_id: this.data.orderDetail.data.store_id,
        update: true,
      },
      type: 'redirectTo'
    })
  }
}))
